<template>
  <div class="login-container">
    <div class="login_box">
      <div class="avatar_box">
        <!-- 头像区域 -->
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 首先为表单绑定一个form对象,然后为每个form-item绑定对应对象中的属性值 -->
      <!-- ref是整个表单对象类似于form的id -->
      <el-form ref="form" label-width="0px" class="login_form" :model="login_form" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="login_form.username">
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="login_form.password" type="password">
          </el-input>
        </el-form-item>
        <!-- 按钮选项 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login" :plain="true">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //登录表单的数据绑定对象
      login_form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //重置登录表单
    reset: function () {
      this.$refs.form.resetFields();
    },
    login: function () {
      //valid就是验证结果
      this.$refs.form.validate(async (valid) => {
        console.log(valid);
        //如果验证通过,就调用axios发起请求
        if (!valid) return;
        //服务器返回的数据中,就只有data是有用数据,其余均无用
        //利用解构把res重新赋值给data
        const { data: res } = await this.$http.post("login", this.login_form);
        console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error('登录失败');
        }
        this.$message.success('登陆成功')
        //将登录成功以后应该将服务器返回的token值存到 sessionstroage 中
        //token 只应该在当前网站打开期间有效, 所以将 token 保存在 sessionstorage 中
        window.sessionStorage.setItem('token', res.data.token)
        //通过编程式导航跳转到后台主页, 路由地址是 /home
        this.$router.push('/home')
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
}
</style>
